﻿using Microsoft.EntityFrameworkCore;

namespace EFCoreDemo.Api.Models
{
    public class AppDbContext : DbContext
    {
        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
        {
        }

        public DbSet<Order> Orders { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Address> Addresses { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Address>().ToTable("Address");

            modelBuilder.Entity<Order>()
                .HasOne(e => e.Address)
                .WithOne()
                .HasForeignKey<Order>(e => e.AddressId)
                .HasConstraintName("FK_Test")
                .IsRequired();

            modelBuilder.Entity<User>()
                .HasOne(e => e.Address)
                .WithOne()
                .HasForeignKey<User>(e => e.AddressId)
                .IsRequired();
        }
    }
}
